﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using CoreMVVM.ViewModels;
using GeometryViz3D.Model;
using System.Windows.Input;
using CoreMVVM.Commands;
using System.Collections.ObjectModel;
using System.Windows;
using System.Windows.Data;
using System.ComponentModel;

namespace GeometryViz3D.ViewModels
{
    public class AddLineViewModel : ViewModelBase
    {
        #region Fields

        #region Commands

        /// <summary>
        /// The Command to add a line to the model.
        /// </summary>
        private DelegateCommand m_AddLineCommand;

        #endregion

        G3DModel m_model;

        ObservableCollection<G3DPoint> m_points;
        ObservableCollection<string> m_colors;

        private string m_color = "Black";

        private G3DPoint m_point1;
        private G3DPoint m_point2;

        #endregion

        public AddLineViewModel(G3DModel model)
        {
            m_model = model;

            m_points = new ObservableCollection<G3DPoint>(model.Points);
            m_colors = new ObservableCollection<string>(G3DColors.Colors);
        }

        #region Properties

        public G3DModel Model
        {
            get { return m_model; }
            set { m_model = value; }
        }

        public ObservableCollection<G3DPoint> Points
        {
            get { return m_points; }
        }

        public ObservableCollection<string> LineColors
        {
            get { return m_colors; }
        }

        public string Color
        {
            get { return m_color; }
            set
            {
                m_color = value;
                OnPropertyChanged("Color");
            }
        }

        public G3DPoint Point1
        {
            get { return m_point1; }
            set
            {
                m_point1 = value;
                OnPropertyChanged("Point1");
            }
        }

        public G3DPoint Point2
        {
            get { return m_point2; }
            set
            {
                m_point2 = value;
                OnPropertyChanged("Point2");
            }
        }

        #region Commands

        /// <summary>
        /// The Command to add a line to the model.
        /// </summary>
        public ICommand AddLineCommand
        {
            get
            {
                if (m_AddLineCommand == null)
                {
                    m_AddLineCommand = new DelegateCommand(AddLine, CanAddLine);
                }

                return m_AddLineCommand;
            }
        }

        #endregion

        #endregion

        #region Event Handlers

        private bool CanAddLine()
        {
            return m_point1 != null && m_point2 != null;
        }

        private void AddLine()
        {
            try
            {
                m_model.AddLine(m_point1, m_point2, G3DColors.GetColor(m_color));
                //OnPropertyChanged("Model");
            }
            catch (G3DException ex)
            {
                MessageBox.Show(ex.Message, "GeometryViz3D");
            }
        }

        #endregion
    }
}
